package com.yz.admin.mapper;

import com.yz.admin.vo.SubmitVo;
import com.yz.common.pojo.DbType;
import com.yz.common.pojo.Task;
import com.yz.admin.pojo.User;
import com.yz.common.pojo.DbLink;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface AdminMapper {
    //修改用户状态(假删除)
    @Update("UPDATE t_user SET  status=#{status} WHERE id=#{id}")
    int deleteUserById(Integer id,String status);

    //审批任务
    @Update("UPDATE t_task SET status=#{status},solveId=#{userId},pdate=#{pdate} where id=#{id}")
    int updateTaskStatus(SubmitVo submitVo);

    //查找账号
    @Select("select * from t_user where status=#{status} ")
    List<User> findAllUser(String status);

    //修改权限
    @Update("UPDATE t_user SET `roleid`=#{roleid},`roles`=#{roles} WHERE (`id`=#{id})")
    void updateUser(User user);

    //查找角色
    @Select("select * from t_role where id=#{value}")
    Map findbyRole(Integer roleid);

    //查询所有任务
    @Select("select * from t_task where dstatus=1")
    List<Task> findAllTask();

    //添加数据库连接
    @Insert("INSERT INTO t_dblink (`driver`, `ip`, `port`, `dbType`, `linkName`, `dbTypeName`, `dstatus`) VALUES (#{driver}, #{ip}, #{port}, #{dbType}, #{linkName}, #{dbTypeName}, 1)")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    void addDatabaseLink(DbLink dbLink);

    //查询数据库类型名根据数据库类型id
    @Select("SELECT typeName FROM t_dbtype where id = #{value} and dstatus=1")
    String getDbTypeNameByDbTypeId(Integer dbType);

    //查询数据库连接根据id
    @Select("select * from t_dblink where id = #{value} and dstatus=1")
    DbLink findLinkById(Integer linkId);

    //添加数据库
    @Insert("INSERT INTO t_database (`linkId`, `username`, `pass`, `databaseName`, `dstatus`) VALUES (#{linkId}, #{username}, #{password}, #{databasesName}, '1')")
    void insertDatabase(@Param("linkId") Integer linkId,@Param("username") String username,@Param("password") String password,@Param("databasesName") String databasesName);

    //删除数据库连接
    @Update("UPDATE t_dblink SET dstatus = 0 where id = #{linkId}")
    int deleteDbLinkById(Integer linkId);

    //删除数据库
    @Update("UPDATE t_database SET dstatus = 0 where linkId = #{linkId}")
    void deleteDatabaseByLinkId(Integer linkId);

    //查询用户id
    @Select("select id from t_user where username = #{value}")
    Integer findUserIdByUsername(String username);

    @Select("select * from t_dbtype where dstatus=1")
    List<DbType> getAllDatabaseType();

    @Select("select * from t_dblink where dstatus=1")
    List<DbLink> getAllDbLink();
}

